package com.xbongbong.saas.factory.publicrule;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.sys.domain.entity.UserEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author kaka
 * @date 2021-12-14
 */
public class CheckRuleUtil {

    /**
     * 通过数据库中的配置解析出捞取频率限制值（限制数量）/前负责人限制值（限制天数）
     * @param ruleValueArray 捞取频率限制配置 JSONArray/前负责人限制配置 JSONArray
     * @param user 判断的目标员工
     * @return 捞取频率限制值（限制数量）/前负责人限制值（限制天数）
     */
    public static Integer getMinValue(JSONArray ruleValueArray, UserEntity user) {
        Integer allValue = null;
        Integer specialValue = null;
        List<Long> deptIds;
        List<Integer> roleIds = null;
        try {
            deptIds = JSONArray.parseArray(user.getDepartment(), Long.class);
        } catch (Exception e) {
            deptIds = new ArrayList<>();
        }
        if (deptIds == null) {
            deptIds = new ArrayList<>();
        }

        String role = user.getRoleIds();
        if (StringUtil.isNotEmpty(role)) {
            roleIds = StringUtil.roleIdToListInt(role);
        }
        if (roleIds == null) {
            roleIds = new ArrayList<>();
        }
        if (Objects.nonNull(ruleValueArray)) {
            for(Object object : ruleValueArray) {
                JSONObject json = (JSONObject)object;
                Integer value = json.getInteger("value");
                JSONArray users = json.getJSONArray("users");
                JSONArray depts = json.getJSONArray("depts");
                JSONArray roles = json.getJSONArray("roles");
                if(users == null && depts == null && roles == null){
                    allValue = value;
                    continue ;
                }

                List<String> userIdList = new ArrayList<>();
                List<Long> deptIdList = new ArrayList<>();
                List<Integer> roleIdList = new ArrayList<>();
                if (Objects.nonNull(users)) {
                    userIdList = JSON.parseArray(users.toJSONString(), String.class);
                }
                if (Objects.nonNull(depts)) {
                    deptIdList = JSON.parseArray(depts.toJSONString(), Long.class);
                }
                if (Objects.nonNull(roles)) {
                    roleIdList = JSON.parseArray(roles.toJSONString(), Integer.class);
                }
                deptIdList.retainAll(deptIds);
                roleIdList.retainAll(roleIds);

                boolean configFail = !userIdList.contains(user.getUserId()) && deptIdList.isEmpty() && roleIdList.isEmpty();
                if(configFail){
                    //对于当前用户无效的配置
                    continue ;
                }

                //若不存在对应值或值比当前值大，则用当前值覆盖（获取最小值）
                if(specialValue == null || specialValue > value){
                    specialValue = value;
                }
            }
        }

        return specialValue == null ? allValue : specialValue;
    }
}
